import * as docx from "docx"

const font_size_list = {
    "28号": 56,
    "二号": 44,
    "三号": 32,
    "小四": 24,
    "五号": 21
}
class xzhqDocx {
    constructor(arr) {
        const child_arr = this.iter_arr(arr)
        const doc = new docx.Document({
            sections: [{
                children: child_arr
            }]
        })
        docx.Packer.toBlob(doc).then((blob) => {
            const a = document.createElement("a");
            a.href = URL.createObjectURL(blob);
            a.click();
        });
    }
    iter_arr(arr) {
        const child_arr = []
        arr.forEach(el => {
            if (el.child && el.child.length) {
                let child_label = this.iter_arr(el.child)
                el.style.children = child_label
            }
            const label = this.switch_lable(el.label, new IterObject(el.style))
            child_arr.push(label)
        })
        return child_arr
    }
    switch_lable(label, style) {
        let _label = null
        switch (label) {
            case "p":
                _label = this.p_lable(...style)
                break
            case "text":
                _label = this.text_lable(...style)
                break
            case "line":
                _label = this.line(...style)
                break
            case "line_space":
                _label = this.line_space(...style)
                break
            case "img":
                _label = this.img_p_lable(...style)
                break
            case "table":
                _label = this.table_lable(...style)
                break
            case "table_row":
                _label = this.table_row(...style)
                break
            case "table_cell":
                _label = this.table_cell(...style)
                break
            case "page":
                _label = this.add_page()
                break
            default:
                console.log("未匹配到");
                break
        }
        return _label
    }
    /**
     * 
     * @param {*} align 对其方向 全部大写 center start end both JUSTIFIED {两端对齐 还是会贴在一起  需要自己加间隔文本）
     * @returns heading null(正文) HEADING_1(标题1), HEADING_2, HEADING_3, HEADING_4, HEADING_5, HEADING_6, TITLE
     */
    p_lable(align, children = [], heading = null) {
        const p = new docx.Paragraph({
            alignment: docx.AlignmentType[align],   //文本对齐方向
            heading,       //该文本类型
            spacing: {
                line: 580, // 设置行间距为 29 磅（29 * 20 = 580 twips）
                lineRule: "exact", // 设置行间距规则为固定值
            },
            children
        })
        return p
    }
    /**
     * 
     * @param {*} img 图片bolb
     * @param {*} width 图片宽度
     * @param {*} height 图片高度
     */
    img_p_lable(img, width, height) {
        const p = new docx.Paragraph({
            alignment: docx.AlignmentType.CENTER,
            children: [
                new docx.TextRun({
                    text: "ceshi"
                }),
                new docx.ImageRun({
                    type: "png",
                    data: img,
                    transformation: {
                        width,
                        height,
                    },
                })
            ]
        })
        return p
    }
    /**
     * @param {*} text 文本内容
     * @param {*} font 字体类型
     * @param {*} size_key 字体大小key
     * @param {*} bold 是否加粗
     * @param {*} color 颜色
     * @returns 
     */
    text_lable(text, font = "宋体", size_key = "五号", bold = false, color = "#000000") {
        const size = font_size_list[size_key]
        const span = new docx.TextRun({
            text,
            bold,
            font,
            color,
            size
        })
        return span
    }
    line(color, width = 6) {
        const p = new docx.Paragraph({
            border: {
                bottom: {
                    color,
                    space: 1,
                    style: "inset",
                    size: width,
                }
            }
        })
        return p
    }
    /**
     * 
     * @param {*} line 行间距
     * @returns 
     */
    line_space(line = 300) {
        const p = new docx.Paragraph({
            alignment: docx.AlignmentType.CENTER,   //文本对齐方向
            spacing: {
                line, // 设置行间距为 29 磅（29 * 20 = 580 twips）
                lineRule: "exact", // 设置行间距规则为固定值
            }
        })
        return p
    }
    table_lable(children, size = 9000) {
        const table = new docx.Table({
            alignment: docx.AlignmentType.CENTER,
            width: {
                size,
                type: docx.WidthType.DXA,
            },
            rows: children
        })
        return table
    }
    table_row(children) {
        const row = new docx.TableRow({
            children
        })
        return row
    }
    table_cell(children, borders = {}) {
        const cell = new docx.TableCell({
            borders,
            children,
        })
        return cell
    }
    add_page() {
        const p = new docx.Paragraph({
            children: [
                new docx.PageBreak()
            ]
        })
        return p
    }
}

export default xzhqDocx

class IterObject {
    data
    constructor(obj) {
        const arr = []
        for (const [key, val] of Object.entries(obj)) {
            arr.push(val)
        }
        this.data = arr
    }
    [Symbol.iterator]() {
        let index = 0;
        const data = this.data

        return {
            next() {
                if (index < data.length) {
                    return { value: data[index++], done: false };
                } else {
                    return { done: true };
                }
            }
        }
    }
}
